package com.example.demo.dao;

import com.example.demo.pojo.Student;
import com.example.demo.pojo.dto.Borrow;
import com.example.demo.pojo.vo.StudentBorrowVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author chenk
 * @description 功能
 */
@Repository
public interface StudentDao {
    /**
     * find Stu for login
     * @param id // stu IO
     * @param password
     * @return
     */
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
    })
    @Select("select * from student where id = #{id} and password = #{password}")
    Student findStudentByIdAndPassword(@Param("id") Integer id, @Param("password") String password);

    /**
     * 借阅
     * @param borrow 传入借阅记录
     * @return
     */
    @Transactional
    @Insert("insert into borrow value(#{studentId}, #{bookId}, #{borrowDate}), #{backDate}")
    int insertBorrowed(Borrow borrow);
// 查询语句
    @Results({
            @Result(property = "book_id", column = "id"),
            @Result(property = "book_name", column = "name"),
            @Result(property = "author", column = "author"),
            @Result(property = "subject", column = "subject"),
            @Result(property = "borrow_date", column = "borrowDate"),
            @Result(property = "back_date", column = "backDate"),
    })
    @Select("select book_id, book_name, author,subject, b2.borrow_date, b2.back_date " +
            "from book b1 left join borrow b2 on b1.book_id = b2.book_id " +
            "where = b1.student_id = #{stuId} ")
    List<StudentBorrowVo> findBorrowBooks(Integer stuId);
}
